﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using Sendz.Validators;

namespace Sendz.Domain
{
    public class FormField
    {
        // Entity
        [HiddenInput(DisplayValue = false)]
        public Guid FormFieldKey { get; set; }

        [DisplayName("Form Field Name")]
        [Required(ErrorMessage = "A name for your form field is required")]
        [StringLength(255, ErrorMessage = "Please ensure that the form field name you enter is less than 255 characters")]
        public string Name { get; set; }

        [HiddenInput(DisplayValue = false)]
        public bool IsRemoved { get; set; }

        [DisplayName("Required?")]
        public bool IsRequired { get; set; }

        [DisplayName("Options (1 Per Line)")]
        [DataType(DataType.MultilineText)]
        [RequiredWhenContains("ListEnum", "choice", ErrorMessage = "When your field requires a choice, you must enter at least one option")]
        public string Options { get; set; }

        [HiddenInput(DisplayValue = false)]
        public int SortOrder { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime LastModified { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime CreatedOn { get; set; }

        // Parents
        [HiddenInput(DisplayValue = false)]
        public Guid FormKey { get; set; }

        [HiddenInput(DisplayValue = false)]
        public Form Form { get; set; }

        // Used for custom validation pattern (string based validation is more natural than Guid)
        [DisplayName("Field Type")]
        public string ListEnum { get; set; }

        [HiddenInput(DisplayValue = false)]
        public string RegEx { get; set; }

        [HiddenInput(DisplayValue = false)]
        public string RegExError { get; set; }

        [HiddenInput(DisplayValue = false)]
        public Guid FieldTypeKey { get; set; }

        [HiddenInput(DisplayValue = false)]
        public FieldType FieldType { get; set; }

        // Children
        [HiddenInput(DisplayValue = false)]
        public List<ResponseItem> ResponseItems { get; set; }

        // Constructor
        public FormField()
        {
            this.FormFieldKey = Guid.NewGuid();
            this.CreatedOn = DateTime.Now;
        }

        public FormField(Guid formKey)
        {            
            this.FormFieldKey = Guid.NewGuid();
            this.FormKey = formKey;
            this.CreatedOn = DateTime.Now;
        }

        public FormField(string name, bool isRequired, string options)
        {
            this.FormFieldKey = Guid.NewGuid();
            this.Name = name;
            this.IsRequired = isRequired;
            this.Options = options;
            this.CreatedOn = DateTime.Now;
        }
    }
}
